package com.haruhinav.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@RestController
@RequestMapping("/file")
public class FileController {


//    多图片上传
@PostMapping("/a/upload/v1")
    public Map<String, Object> upload(@RequestParam("file") List<MultipartFile> files, HttpServletRequest request,
                                       HttpServletResponse response)throws IOException{
    Map<String, Object> map = new HashMap<>();


    String path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\upload\\";

    //FileOutputStream output = null;
    ArrayList filenames = new ArrayList();
    try {
        if (files != null && files.size() > 0) {
            for (MultipartFile fileObj : files) {
                //随机生成一个新名字,转换成string,并将其中的"-"换成""
                String uuid = UUID.randomUUID().toString().replace("-", "");
                //获取上传文件的后缀
                String fileName = fileObj.getOriginalFilename();
                String[] strArray = fileName.split("\\.");
                int suffixIndex = strArray.length -1;
                String fileType =strArray[suffixIndex];

                //String fileType = fileObj.getOriginalFilename().split("\\.")[1];
                String newFileName = uuid+"."+fileType;

               // System.out.println("newFileName---------------->>"+newFileName);

                //output = new FileOutputStream(path + uuid + "." + fileType);
               // String url = "/src/main/resources/static/upload/";

                //IOUtils.copy(fileObj.getInputStream(), output);
                try{
                    System.out.println(path + newFileName);
                    File dest = new File(new File(path) + "\\" + newFileName);
                    fileObj.transferTo(dest);
                    filenames.add(newFileName);
                    //System.out.println(path +newFileName);

                }catch (Exception e){
                    map.put("errorCode", "0");
                    map.put("errorMsg", "error");
                    e.printStackTrace();
                }
            }
        }
        map.put("errorCode", "-1");
        map.put("errorMsg", "success");
        map.put("fileNames",  filenames);
        return map;
    } catch (Exception e) {
        e.printStackTrace();
        map.put("errorCode", "0");
        map.put("errorMsg", "error");
    }
    return map;
    }

//单图片上传
    @PostMapping("/a/upload/v2")
    public Map<String,String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        Map<String, String> map = new HashMap<>();
        if (file.isEmpty()) {
            map.put("code","0");
            map.put("msg","无文件！");
            return map;
        }

        String fileName = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");

        String[] strArray = fileName.split("\\.");
        int suffixIndex = strArray.length -1;
        String fileType =strArray[suffixIndex];
        String newFileName = uuid+"."+fileType;


        //通过getAbsolutePath()获取文件的绝对路径
        File dest = new File(new File(System.getProperty("user.dir")
                +"\\src\\main\\resources\\static\\upload\\") + "\\" + newFileName);

//        File dest1 = new File(System.getProperty("user.dir") +"\\src\\main\\resources\\static\\upload\\");
//        if(!dest1.exists()){
//            dest1.mkdir();
//        }
//        file.transferTo(new File(dest1+"\\"+ fileName));
        System.out.println(dest.toString());
        try {
            file.transferTo(dest);
            map.put("code","-1");
            map.put("msg","success");
            map.put("fileName",newFileName);
        } catch (IOException e) {
            map.put("code","1");
            map.put("msg","error");
            map.put("fileName",newFileName);
            e.printStackTrace();

        }
        return map;
    }
}
